package com.ruoyi.web.controller.background;

import com.ruoyi.background.domain.AppLotteryPrize;
import com.ruoyi.background.service.IAppLotteryPrizeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 积分抽奖奖品Controller
 *
 * @author susu
 * @date 2024-08-24
 */
@RestController
@RequestMapping("/applet/appPrize")
public class AppLotteryPrizeController extends BaseController {
    @Autowired
    private IAppLotteryPrizeService appLotteryPrizeService;


    /**
     * 生成预抽奖结果
     *
     * @return 结果
     */
    @GetMapping("/createUserPreLottery")
    public AjaxResult createUserPreLottery() {
        return AjaxResult.success(appLotteryPrizeService.createUserPreLottery());
    }


    @GetMapping("/setNowWinNum")
    public AjaxResult setNowWinNum(){
        return AjaxResult.success(appLotteryPrizeService.setNowWinNum());
    }

    /**
     * 统计用户是否最后一个
     * @return
     */
    @GetMapping("/initUserIsFinale")
    public AjaxResult initUserIsFinale(){
        return AjaxResult.success(appLotteryPrizeService.initUserIsFinale());
    }


    /**
     * 查询积分抽奖奖品列表
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppLotteryPrize appLotteryPrize) {
        startPage();
        List<AppLotteryPrize> list = appLotteryPrizeService.selectAppLotteryPrizeList(appLotteryPrize);
        return getDataTable(list);
    }

    /**
     * 导出积分抽奖奖品列表
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:export')")
    @Log(title = "积分抽奖奖品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppLotteryPrize appLotteryPrize) {
        List<AppLotteryPrize> list = appLotteryPrizeService.selectAppLotteryPrizeList(appLotteryPrize);
        ExcelUtil<AppLotteryPrize> util = new ExcelUtil<AppLotteryPrize>(AppLotteryPrize.class);
        util.exportExcel(response, list, "积分抽奖奖品数据");
    }

    /**
     * 获取积分抽奖奖品详细信息
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appLotteryPrizeService.selectAppLotteryPrizeById(id));
    }

    /**
     * 新增积分抽奖奖品
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:add')")
    @Log(title = "积分抽奖奖品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppLotteryPrize appLotteryPrize) {
        return toAjax(appLotteryPrizeService.insertAppLotteryPrize(appLotteryPrize));
    }

    /**
     * 修改积分抽奖奖品
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:edit')")
    @Log(title = "积分抽奖奖品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppLotteryPrize appLotteryPrize) {
        return toAjax(appLotteryPrizeService.updateAppLotteryPrize(appLotteryPrize));
    }

    /**
     * 修改积分抽奖奖品状态
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:edit')")
    @Log(title = "积分抽奖奖品", businessType = BusinessType.UPDATE)
    @GetMapping("/changeStatus/{id}/{status}")
    public AjaxResult editStatus(@PathVariable Long id, @PathVariable Integer status) {
        return toAjax(appLotteryPrizeService.changeStatus(id, status));
    }

    /**
     * 删除积分抽奖奖品
     */
    @PreAuthorize("@ss.hasPermi('applet:appPrize:remove')")
    @Log(title = "积分抽奖奖品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(appLotteryPrizeService.deleteAppLotteryPrizeByIds(ids));
    }
}
